package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.OrderAdapter;
import com.wnhz.work.yz.bean.OrderBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---全部订单
 */
public class AllOrderActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "AllOrderActivity";
    private TextView title;
    private TextView tv_all, tv_wait_pay, tv_wait_send, tv_wait_true;
    private View line1, line2, line3, line4;
    private int textColorNor, textColorCli;
    private ListView myListView;
    private OrderAdapter mAdapter;
    private List<OrderBean> beanList = new ArrayList<>();
    private String page;
    private int indexPosition, indexPosition2, indexPosition3, index4;

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_order);
        page = getIntent().getStringExtra("Page");
        initView();
        initData();
    }

    private void initData() {
        textColorNor = getResources().getColor(R.color.text);
        textColorCli = getResources().getColor(R.color.main_color);
        if (page.equals("0")) {
            upload("0");
        } else {
            if (page.equals("1")) {
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorCli);
                tv_wait_send.setTextColor(textColorNor);
                tv_wait_true.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
            } else if (page.equals("2")) {
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorNor);
                tv_wait_send.setTextColor(textColorCli);
                tv_wait_true.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                line4.setVisibility(View.INVISIBLE);
            } else if (page.equals("3")) {
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorNor);
                tv_wait_send.setTextColor(textColorNor);
                tv_wait_true.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.VISIBLE);
            }
            upload(page + "");
        }
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单");
        tv_all = (TextView) findViewById(R.id.tv_all);
        tv_wait_pay = (TextView) findViewById(R.id.tv_wait_pay);
        tv_wait_send = (TextView) findViewById(R.id.tv_wait_send);
        tv_wait_true = (TextView) findViewById(R.id.tv_wait_true);
        myListView = (ListView) findViewById(R.id.myListView);

        findViewById(R.id.btn_main_left).setOnClickListener(this);
        tv_all.setOnClickListener(this);
        tv_wait_pay.setOnClickListener(this);
        tv_wait_send.setOnClickListener(this);
        tv_wait_true.setOnClickListener(this);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        int index;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //全部
            case R.id.tv_all:
                tv_all.setTextColor(textColorCli);
                tv_wait_pay.setTextColor(textColorNor);
                tv_wait_send.setTextColor(textColorNor);
                tv_wait_true.setTextColor(textColorNor);

                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                upload("0");
                break;

            //待付款
            case R.id.tv_wait_pay:
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorCli);
                tv_wait_send.setTextColor(textColorNor);
                tv_wait_true.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                upload("1");
                break;

            //待发货
            case R.id.tv_wait_send:
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorNor);
                tv_wait_send.setTextColor(textColorCli);
                tv_wait_true.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                line4.setVisibility(View.INVISIBLE);
                upload("2");
                break;

            //待确认
            case R.id.tv_wait_true:
                tv_all.setTextColor(textColorNor);
                tv_wait_pay.setTextColor(textColorNor);
                tv_wait_send.setTextColor(textColorNor);
                tv_wait_true.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.VISIBLE);
                upload("3");
                break;
            case R.id.ll_top1:
                index = (int) v.getTag();
                intent = new Intent(AllOrderActivity.this, OrderDetailsActivity.class);
                intent.putExtra("orderId", beanList.get(index).getOrder_id());
                startActivity(intent);
                break;
            //去支付
            case R.id.tv_go_pay:
                index = (int) v.getTag();
                intent = new Intent(AllOrderActivity.this, ChoosePayActivity.class);
                intent.putExtra("order_no", beanList.get(index).getOrder_no());
                startActivity(intent);
                break;

            //取消订单
            case R.id.tv_cancel:
                index = (int) v.getTag();
                indexPosition = index;
                if ("1".equals(beanList.get(index).getOrder_status())) {
                    AllOrderActivity.this.showDialogv7("确定取消该订单？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition).getOrder_id();
                            upcancel(orderId);
                        }
                    });
                } else {
                    MyToast("该订单不可取消");
                }
                break;

            //删除订单
            case R.id.tv_delete:
                index = (int) v.getTag();
                indexPosition2 = index;
                if ("0".equals(beanList.get(index).getOrder_status()) || "4".equals(beanList.get(index).getOrder_status()) || "5".equals(beanList.get(index).getOrder_status())) {
                    showDialogv7("确定删除该订单？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition2).getOrder_id();
                            updelete(orderId);
                        }
                    });

                } else {
                    MyToast("该订单不可删除");
                }
                break;

            //订单详情
            case R.id.ll_top:
                index = (int) v.getTag();
                intent = new Intent(AllOrderActivity.this, OrderDetailsActivity.class);
                intent.putExtra("orderId", beanList.get(index).getOrder_id());
                startActivity(intent);
                break;

            //取消退款
            case R.id.tv_no_refund:
                index = (int) v.getTag();
                indexPosition3 = index;
                if ("6".equals(beanList.get(index).getOrder_status())) {
                    showDialogv7("确定取消退款？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition3).getOrder_id();
                            uprefund(orderId);
                        }
                    });
                } else {
                    MyToast("该订单不可取消");
                }
                break;
            //取消退款
            case R.id.buy_two:
                index = (int) v.getTag();

                if ("0".equals(beanList.get(index).getOrder_status()) || "4".equals(beanList.get(index).getOrder_status()) || "5".equals(beanList.get(index).getOrder_status())) {
                    String orderId = beanList.get(index).getOrder_id();
                    buyTwo(orderId);
                } else {
                    MyToast("商品已过期");
                }
                break;
            //提醒发货
            case R.id.shipments:
                index = (int) v.getTag();
                if ("2".equals(beanList.get(index).getOrder_status())) {
                    String orderId = beanList.get(index).getOrder_id();
                    upLoadShipments(orderId);
                }
                break;
            //进度
            case R.id.shop_progress:
                index = (int) v.getTag();
                if ("2".equals(beanList.get(index).getOrder_status()) || "3".equals(beanList.get(index).getOrder_status())) {
                    intent = new Intent(AllOrderActivity.this, ShopProgressActivity.class);
                    String orderId = beanList.get(index).getOrder_id();
                    intent.putExtra("orderId", orderId);
                    startActivity(intent);
                    //shopProgress(orderId);
                }
                break;
            //申请退款
            case R.id.tv_refund1:
                index = (int) v.getTag();
                index4 = index;
                showDialogv7("确认申请退款？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if ("2".equals(beanList.get(index4).getOrder_status()) || "3".equals(beanList.get(index4).getOrder_status())) {
                            String orderId = beanList.get(index4).getOrder_id();
                            uprefund2(orderId);
                        }
                    }
                });

                break;
            default:
                break;
        }
    }

    public void upload(final String type) {
        beanList.clear();
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        XUtil.Post(Url.ORDER_GETORDERLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("=AllOrderActivity订单==", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (res.equals("1")) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            OrderBean bean = new OrderBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), OrderBean.class);
                            beanList.add(bean);
                        }
                        setData();
                    } else if (res.equals("0")) {
                        MyToast(jsonObject.optString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
                Log.e(TAG, "onError: " + ex);
            }
        });
    }

    private void setData() {
        mAdapter = new OrderAdapter(this, beanList, this);
        myListView.setAdapter(mAdapter);
    }

    private void upLoadShipments(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_REMINDDELIVERY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:REFUNDORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload(type);
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void buyTwo(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_AGAINBUY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:CANCELREFUND== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        if (mAdapter != null) {
                            mAdapter.notifyDataSetChanged();
                        }
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void uprefund2(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_REFUNDORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:REFUNDORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload("0");
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void uprefund(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_CANCELREFUND, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:CANCELREFUND== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload("0");
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });

    }

    private void updelete(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_DELORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:ORDER_DELORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload("0");
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void upcancel(String orderId) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_CANCELORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: orderId=== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload("0");
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

}
